package eu.runelord.programmers.t139127;

import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.net.MalformedURLException;
import java.net.URL;
// $Id$
public class Main2 extends Applet{
	private static final long serialVersionUID = 1L;
	private Obrazek obrazek;
	private Painter painter;

	public void init() {
		try {
			obrazek = new Obrazek(new URL("http://musicbox24.pl/"),
					"image.php?mode=band_image&band_id=1588&width=50", 100, 100);
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		painter = new Painter(obrazek, this);
		addKeyListener(new Sterowanie(obrazek, this));
		setSize(300, 300);
		setVisible(true);
		repaint();
	}

	public void paint(Graphics g) {
		painter.paint(g);
	}
}

class Painter{
	
	private Obrazek obrazek;
	private Image image;

	public Painter(Obrazek obrazek, Main2 main2) {
		super();
		this.obrazek = obrazek;
		image = main2.getImage(obrazek.url, obrazek.name);
	}

	public void paint(Graphics g){
		g.setColor(Color.blue);
        g.drawString("Wartość x: ", 20, 250);
        g.drawString("Wartość y: ", 20, 260);
        g.drawImage(image, obrazek.x, obrazek.y, null);
	}
}

class Sterowanie extends KeyAdapter {

	private Obrazek obrazek;
	private Main2 main2;
	
	public Sterowanie(Obrazek obrazek, Main2 main2) {
		super();
		this.obrazek = obrazek;
		this.main2 = main2;
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (e.getKeyCode() == KeyEvent.VK_UP) {
			obrazek.y += 5;
			main2.repaint();
		}
		if (e.getKeyCode() == KeyEvent.VK_DOWN) {
			obrazek.y -= 5;
			main2.repaint();
		}
		if (e.getKeyCode() == KeyEvent.VK_LEFT) {
			obrazek.x += 5;
			main2.repaint();
		}
		if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
			obrazek.x -= 5;
			main2.repaint();
		}
	}

}

class Obrazek {
	public URL url;
	public String name;
	public int x;
	public int y;

	public Obrazek(URL url, String name, int x, int y) {
		this.url = url;
		this.name = name;
		this.x = x;
		this.y = y;
	}
}